//
//  ApiManager.m
//  GoftApp
//
//  Created by admin on 15/9/6.
//  Copyright (c) 2015年 mohe. All rights reserved.
//

#import "ApiManager.h"

#pragma mark APIError
@interface APIError : NSError

@end
@implementation APIError

@end

#pragma mark ApiManager
@implementation ApiManager

IMPLEMENT_SINGLETON(ApiManager)

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString *baseUrl = [NSString stringWithFormat:@"http://%@/", API_SERVERIP];
        self = [[ApiManager alloc]initWithBaseURL:[NSURL URLWithString:baseUrl]];
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        self.requestSerializer.timeoutInterval = 20.0f;
        self.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    return self;
}
//验证服务器返回的数据
+ (NSError *)analysisData:(id)data
{
    //如果不是字典 证明服务器返回数据的格式错误
    if(![data isKindOfClass:[NSDictionary class]]){
        APIError *error = [[APIError alloc]initWithDomain:kErrorDomain code:-2 userInfo:nil];
        return error;
    }
    //如果Result 不等于 0 证明请求失败 有错误码
    if([data[@"error_code"] integerValue] != 0){
        APIError *error = [[APIError alloc]initWithDomain:kErrorDomain code:[data[@"error_code"] integerValue] userInfo:nil];
        return error;
    }
    return nil;
}

- (NSURLSessionDataTask *)post:(NSString *)url
  parameters:(NSDictionary *)parameters
   OnSuccess:(void (^)(id))successHandler
   onFailure:(void (^)(NSError *))failureHandler
{
   NSURLSessionDataTask *task = [self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = [ApiManager analysisData:responseObject];
        if(error){
            if(failureHandler){
                failureHandler(error);
            }
        }else{
            successHandler(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        if ([AppUnit shared].networkStatus <= 0) {
            APIError *error = [[APIError alloc]initWithDomain:kErrorDomain code:404 userInfo:nil];
            if(failureHandler)
                failureHandler(error);
        }else{
            if(failureHandler)
                failureHandler(error);
        }
    }];
    return task;
}
- (void)post:(NSString *)url parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block OnSuccess:(void (^)(id))successHandler onFailure:(void (^)(NSError *))failureHandler
{
    [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if(block)
            block(formData);
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = [ApiManager analysisData:responseObject];
    
        if(error){
            if(failureHandler){
                failureHandler(error);
            }
        }else{
            successHandler(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if ([AppUnit shared].networkStatus <= 0) {
            APIError *error = [[APIError alloc]initWithDomain:kErrorDomain code:404 userInfo:nil];
            if(failureHandler)
                failureHandler(error);
        }else{
            if(failureHandler)
                failureHandler(error);
        }

    }];
}
@end
